﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindPrimeNumbers
{
    class Check
    {
        public static bool Prime(int i)
        {
            double k = Math.Sqrt(i);  //质数的定义, 可以只计算到开方, 节省计算时间
            for (int j = 2; j <= k; j++)
            {
                if (i % j == 0)
                {
                    return false;
                }
            }
            return true;
        }
        public static bool Integer(string str)
        {
            try
            {
                int i = int.Parse(str); //验证输入数据是否可以转成Int类型
            }
            catch
            {

                return false;
            }

            return true;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This program can help you find prime numbers. -- Written by Yuxuan in C#"); 
            start:
            Console.WriteLine();
            Console.Write("Enter the upper limit...");
            string a=Console.ReadLine();
            bool ic = Check.Integer(a); //使用Check类Integer函数
            if(!ic)
            {
                Console.WriteLine("Please enter an integer!");
                goto start;
            }
            int aa = Convert.ToInt32(a);
            Console.WriteLine();
            Console.WriteLine("Here is the prime numbers not greater than {0}:",a);
            for (int b = 2; b <= aa; b++) //从2开始的质数每个验证
            {
                if (Check.Prime(b))
                {
                    Console.WriteLine("{0}", b);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Press any key to start over...");
            Console.ReadKey();
            goto start;
        }
    }
}
